var BuildConfigProxy    = require('../../proxy/build_cfg.js');
var BuildConfigModel    = require('../../models/build_cfg.js');
var Build               = require('../../models/build.js');
var EventProxy          = require('eventproxy');
var logger              = require('log4js').getLogger();
var builder              = require('./builder');

exports.getConfigList = function(req, res, next){
  BuildConfigModel.find({}).exec(function(err, data){
    if(err){
      return res.format_json(null,500,err);
    }
    return res.format_json(data,0);
  })
}

exports.addBuildConfig = function(req,res,next){
    var cfg_info = req.body;
    BuildConfigProxy.addBuildConfig(cfg_info,function(err, data){
        if(err){
            logger.error(err);
            return res.format_json(null,500,err);
        }
        return res.format_json(data);
    });
};

exports.deleteBuildConfig = function(req,res,next){
    var config_id = req.params.config_id;
    BuildConfigProxy.deleteBuildConfig(config_id,function(err,data){
        if(err){
            logger.error(err);
            return res.format_json(null,500,err);
        }
        res.format_json(data);
    });
};

exports.deleteBuildConfigs = function(req,res,next){
    var config_ids = req.body.config_ids;
    BuildConfigProxy.deleteBuildConfigs(config_ids,function(err,data){
      if(err){
          logger.error(err);
          return res.format_json(null,500,err);
      }
      res.format_json(data);
    });
};

exports.getBuildConfig = function(req,res,next){
    var config_id = req.params.config_id;
    BuildConfigProxy.getBuildConfig(config_id,function(err,data){
        if(err){
            logger.error(err);
            return res.format_json(null,500,err);
        }
        res.format_json(data);
    });
};

exports.editBuildConfig = function(req,res,next){
    var config_id = req.body.config_id;
    var config_info = req.body.config_info;
    BuildConfigProxy.editBuildConfig(config_id,config_info,function(err,data){
        if(err){
            logger.error(err);
            return res.format_json(null,500,err);
        }
        res.format_json(data);
    });
};

exports.start = function(req, res, next){
  var ep = new EventProxy();
  ep.fail(function(err){
    res.format_json(null, 500, 'start build failed.');
  });
  var config_id = req.body.config;
  BuildConfigModel.findById(config_id, ep.done('config_found'));

  ep.on('config_found', function(config){
    if(!config){
      return ep.throw('can not find build config:'+config);
    }
    var build = new Build();
    build.config = config._id;
    build.project = config.project;
      //if(typeof(req.body.name) == "undefined"){
      //    build.name = config.title;
      //}
    build.save(ep.done('build_created'));
  })

  ep.on('build_created', function(build){
    // Build.findById(build._id, function(error, b){
    //   console.log(error);
    //   console.log(b);
    //   res.json({});
    // });
    builder.start(build._id, function(err, data){
      if(err){
        build.state = 'error';
        return build.save(function(){
          //messageBroker.emit('build_state_update',...)
          return ep.throw('start build failed.');
        });

      }
      Build.findById(build._id, function(err, data){
          if(err){
            return res.format_json({success:false},500,err);
          }
          res.format_json({success:true,build:data});
      });
    });
  })
}
